package com.gitee.xmhzzz.component.web.utils;

import com.gitee.xmhzzz.component.common.dto.BaseUserInfo;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;


/**
  * @ClassName
  * @Description
  * @Author wzq
  * @Date 2023/12/18 10:15
  * @Version 1.0
  */
public class RequestContext {
    private HttpServletResponse response;

    private HttpServletRequest request;

    private Map<String, Object> contextParam;

    private BaseUserInfo userInfo;

    public RequestContext() {
        contextParam = new HashMap<>();
    }

    public HttpServletResponse getResponse() {
        return response;
    }

    public RequestContext setResponse(HttpServletResponse response) {
        this.response = response;
        return this;
    }

    public HttpServletRequest getRequest() {
        return request;
    }

    public RequestContext setRequest(HttpServletRequest request) {
        this.request = request;
        return this;
    }


    public RequestContext put(String name, Object value) {
        contextParam.put(name, value);
        return this;
    }

    public Object get(String name) {
        return contextParam.get(name);
    }

    @SuppressWarnings("unchecked")
    public <T> T get(String name, Class<T> clazz) {
        Object value = contextParam.get(name);
        if (value == null) return null;
        if (!clazz.isInstance(value)) {
            return null;
        }
        return (T) value;
    }

    public boolean inited() {
        return request != null;
    }

    public BaseUserInfo getUserInfo() {
        return userInfo;
    }

    public void setUserInfo(BaseUserInfo userInfo) {
        this.userInfo = userInfo;
    }
}
